package com.lzw.until;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUntil {
    private static SqlSessionFactory factory = null;
    private static InputStream is = null;

    /*执行一次，静态代码块*/
    static {
        try {
            is = Resources.getResourceAsStream("mybatisConfig.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //创建一个sql的会话窗口的工厂
        factory = new SqlSessionFactoryBuilder().build(is);
    }
        //定义一个方法[静态方法通过类名调用，否则要创建对象调用,new对象]
        public static SqlSession getSession () {
            //
            return factory.openSession();
        }
    }